package com.fanfan.controller;

import com.fanfan.enums.AppHttpCodeEnum;
import com.fanfan.exception.SystemException;
import com.fanfan.pojo.Order;
import com.fanfan.pojo.Trade;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.service.OrderService;
import com.fanfan.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    TradeService tradeService;
    @PostMapping
    public ResponseResult addOrder(@RequestBody Order order){
        Trade trade = tradeService.getById(order.getTradeId());
        if (trade.getCounts()>=1){
        trade.setCounts(trade.getCounts()-1);
        tradeService.updateById(trade);
        orderService.save(order);
        return ResponseResult.okResult();
        }else{
            throw new SystemException(AppHttpCodeEnum.TRADE_EXIT);
        }
    }
    @GetMapping
    public ResponseResult orderList(Integer pageNum,Integer pageSize){
        return orderService.orderList(pageNum,pageSize);
    }
    @DeleteMapping("/{id}")
    public ResponseResult refuseOrder(@PathVariable Long id){
        Long tradeId = orderService.getById(id).getTradeId();
        Trade trade=tradeService.getById(tradeId);
        int counts= trade.getCounts()+1;
        trade.setCounts(counts);
        tradeService.updateById(trade);
        orderService.getBaseMapper().deleteById(id);
        return ResponseResult.okResult();
    }
    @PutMapping("/{id}")
    public ResponseResult changOrderStatus(@PathVariable Long id){
        Order order = orderService.getById(id);
        order.setStatus("1");
        orderService.updateById(order);
        return ResponseResult.okResult();
    }
}
